<template>
  <!-- 货运单签收 -->
  <div>
    <div style="margin-bottom: 15px;">
      <el-form ref="form" label-width="80px">
        <el-form-item label="签收日期">
          <el-date-picker v-model="signDate" type="date" placeholder="选择日期时间" :picker-options="pickerOptions" :readonly="isView"></el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <FilterTable
      ref="signTable"
      :columns="columns"
      :fetchapi="fetchapi"
      :params="fetchParams"
      height="auto"
      maxHeight="350"
      :datakey="'rows'"
      columnsRef="partPartInventoryPartSignEditPartOrderSign"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="handleTableDataChange"
    ></FilterTable>
    <!-- 底部单条详情 奥迪客制化隐藏 hexingjia 2019/10/10 -->
    <!-- <div class="box">
      <FormPanel
        ref="formAddWayBill"
        :list="formList"
        :cols="3"
        :labelWidth="100"
        :isSubmitBtn="false"
      />
    </div>-->
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="!isView" type="primary" :loading="signBtnLoading" @click="saveDialog">签收</el-button>
    </div>
  </div>
</template>

<script>
import { getWayBillDetail, wayBillSign, queryTex } from '@/api/part/partInventory/partSign';

import { formatdate, notifyAction } from '@/utils';

export default {
  name: 'editPartOrderSign',
  components: {},
  props: {
    id: String,
    billNo: String,
    isView: Boolean,
    billSignDate: String
  },
  data() {
    return {
      formList: this.createFormList(),
      columns: this.createTableColumns(), // 配件列表字段
      fetchapi: getWayBillDetail,
      fetchParams: { xhrAbort: true },
      pickerOptions: this.createPickerOptions(), // 日期选择器限制时间选择范围
      signDate: '', // 签收日期
      signBtnLoading: false, // 签收按钮加载状态
      tableData: [], // table数据
      tax: 0 // 税率
    };
  },
  computed: {},
  watch: {},
  mounted() {
    // 初始化查询
    this.fetchParams = { xhrAbort: false, billNo: this.billNo };

    // 设置日期为当前日期
    if (!this.isView) {
      this.setDateToToday();
    } else {
      this.setdateToSignDate();
    }
  },
  created() {
    this.queryText();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '配件代码',
          dataIndex: 'matnr',
          width: 150,
          sorter: true
        },
        {
          title: '配件名称',
          dataIndex: 'arktx',
          width: 150,
          sorter: true
        },
        {
          title: '采购价格',
          dataIndex: 'netprHasTax',
          width: 150,
          sorter: true
        },
        {
          title: '不含税采购价',
          dataIndex: 'netpr',
          width: 150,
          sorter: true
        },
        // {
        //   title: '订货数量',
        //   dataIndex: 'lfimg',
        //   width: 150,
        //   sorter: true,
        //   summation: true
        // },
        {
          title: '到货数量',
          dataIndex: 'lfimg',
          width: 150,
          sorter: true,
          summation: true
        },
        // {
        //   title: '建议数量',
        //   dataIndex: 'suggestqty',
        //   width: 150,
        //   sorter: true,
        //   summation: true
        // },
        {
          title: '入库数量',
          dataIndex: 'inCount',
          width: 150,
          sorter: true,
          summation: true
        },
        // {
        //   title: '需求日期',
        //   dataIndex: 'demand',
        //   width: 150,
        //   sorter: true,
        // },
        {
          title: '单据编号',
          dataIndex: 'vbeln',
          width: 150,
          sorter: true
        },
        {
          title: '不含税发货金额',
          dataIndex: 'netprTotal',
          width: 150,
          sorter: true,
          summation: true,
          precision: 2
        },
        {
          title: '含税发货金额',
          dataIndex: 'netprHasTaxTotal',
          width: 150,
          sorter: true,
          summation: true,
          precision: 2
        },
        {
          title: '发货税额',
          dataIndex: 'tax',
          width: 150,
          sorter: true,
          summation: true,
          precision: 2
        },
        {
          title: '行号',
          dataIndex: 'posnr',
          width: 150,
          sorter: true
        }
        // {
        //   title: '总共行数',
        //   dataIndex: 'allRow',
        //   width: 150,
        //   sorter: true
        // },
        // {
        //   title: 'R3SPA',
        //   dataIndex: 'R3SPA',
        //   width: 150,
        //   sorter: true
        // }
      ];
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: 'PANDA订单号',
          fieldName: 'PANDA订单号',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'DMS订单号',
          fieldName: 'ORDER_NOs',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件代码',
          fieldName: 'PART_NUMBER',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'PART_DESCRIPTION',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '订单配件数量',
          fieldName: 'PART_QUANTITY',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '已签收数量',
          fieldName: 'SIGN_COUNT',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '入账数量',
          fieldName: 'IN_QUANTITY',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件单价',
          fieldName: 'LINE_ITEM_UNIT_PRICE',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '实发配件数量',
          fieldName: 'PART_QUANTITY_SHIPPED',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件总金额',
          fieldName: 'LINE_ITEM_EXTENDED_PRICE',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '折扣比例',
          fieldName: 'DISCOUNT_PERCENTAGE',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '销售税是否适用',
          fieldName: 'PART_SALES_TAX_FLAG',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '原配件代码',
          fieldName: 'FORMER_PART_NUMBER',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件销售税率',
          fieldName: 'PART_SALES_TAX_RATE',
          placeholder: '',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'ORIGINAL_ORDER_TYPE',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '是否作废',
          fieldName: 'is_EXCEPTION_REASON',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '提示代码',
          fieldName: 'PARTS_ORDER_LINE_ITEM_COMMENTS',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: 'PANDA注册号',
          fieldName: 'MANUFACTURER_ORDER_NUMBER',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '装运的箱号',
          fieldName: 'CASE_CARTON_NUMBER',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '配件保证金',
          fieldName: 'PART_DEPOSIT_AMOUNT',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '紧急订单服务费',
          fieldName: 'ORDER_ADMINISTRATIVE_CHARGE_AMOUNT',
          placeholder: '',
          disabled: true
        },
        {
          type: 'DATE',
          label: 'PANDA开票日期',
          fieldName: 'PART_ORDER_DATE',
          placeholder: '',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '是否订货零件',
          fieldName: 'IS_ORDER_PARTS',
          placeholder: '',
          disabled: true
        }
      ];
    },
    createPickerOptions() {
      return {
        // 限制时间选择
        disabledDate(time) {
          return time.getTime() > Date.now() || time.getTime() < Date.now() - 1000 * 60 * 60 * 24 * 7;
        }
      };
    },
    // table数据改变
    handleTableDataChange(e) {
      this.tableData = e;
      this.recomputeTableData();
    },
    // 查询税率数据
    async queryText() {
      let res = await queryTex();
      this.tax = res.data;
    },
    // 重新计算table数据
    async recomputeTableData() {
      let tax = this.tax || (await queryTex()).data;
      this.tableData.map(item => {
        item.netprTotal = (item.netpr * item.lfimg).toFixed(2); // 不含税总金额
        item.netprHasTax = (item.netpr * (1 + tax * 1)).toFixed(2); // 含税价格
        item.netprHasTaxTotal = (item.netprHasTax * item.lfimg).toFixed(2); // 含税总金额
        item.tax = (item.netprHasTaxTotal - item.netprTotal).toFixed(2); // 税额
      });
      console.log('重新计算 查询税额', tax);
    },
    // 获取今天日期
    setDateToToday() {
      this.signDate = formatdate(new Date(), 1);
    },
    // 设置日期为签收日期
    setdateToSignDate() {
      console.log(this.billSignDate);
      if (this.billSignDate) {
        let date1 = this.billSignDate.split('.')[0];
        let date = date1.split('+')[0];
        this.signDate = date;
      }
    },
    // 签收
    chargeOff() {},
    async saveDialog() {
      if (this.signBtnLoading) return;
      this.signBtnLoading = true;
      try {
        let res = await wayBillSign({
          id: this.id,
          signStatus: 97091002,
          signDate: this.signDate,
          billNo: this.billNo
        });
        this.signBtnLoading = false;
        if (res.resultCode == 200) {
          notifyAction('签收成功');
          this.closeDialog();
          this.$emit('reSearchTable');
        }
      } catch (err) {
        this.signBtnLoading = false;
        console.log(err);
      }
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped>
.box {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  margin-top: 20px;
}
</style>
